// login业务模块
console.log( 'login业务模块' )

import axios from "axios"

// 获取登录成功后的跳转地址
let target = localStorage.getItem('target')

let form = document.querySelector('.form-signin')
let user = document.querySelector('#user')
let pass = document.querySelector('#pass')

form.onsubmit = function (e){
    // 阻止表单提交
    e.preventDefault()

    // 获取账号密码
    let user_val = user.value
    let pass_val = pass.value

    // 空值判断
    if (!user_val || !pass_val) {
        alert('输入内容不能为空！')
        return
    }

    // 发送登录请求
    axios({
        method: 'post',
        url: 'http://localhost:8080/users/login',
        data: {
            user: user_val,
            pass: pass_val
        }
    })
    .then((res)=>{
        alert(res.data.msg)
        if (res.data.code === 1) {
            // 登录成功，存储token，跳转到target页面
            localStorage.setItem('token',res.data.token)
            location.href = target
        }
    })
    .catch(()=>{
        alert('网络超时，请重试！')
    })
}
